package cn.lynch.bbs.dao;

import java.util.Collection;

public class Page<T> {

	private Collection<T> records;
	private int totalRecords;

	private int currentPage;
	private int pageSize;
	private int totalPages;

	public Collection<T> getRecords() {
		return records;
	}

	public int getTotalRecords() {
		return totalRecords;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getTotalPages() {
		return totalPages;
	}
	
	public int getPageSize() {
		return pageSize;
	}

	public Page(Collection<T> records, int totalRecords, int currentPage,
			int pageSize) {
		super();
		this.records = records;
		this.totalRecords = totalRecords;
		this.currentPage = currentPage;
		this.pageSize = pageSize;
		calculateTotalPages();
	}

	public Page(Collection<T> records, int totalRecords, int currentPage) {
		this(records, totalRecords, currentPage, 10);
	}

	private void calculateTotalPages() {
		this.totalPages = (totalRecords % pageSize == 0) 
			? totalRecords / pageSize : totalRecords / pageSize + 1;
	}
	
	public int getPreviousPage() {
		return isFirst() ? getFirstPage() : currentPage - 1;
	}
	
	public int getNextPage() {
		return isLast() ? getLastPage() : currentPage + 1;
	}
	
	public int getFirstPage() {
		return 1;
	}
	
	public int getLastPage() {
		return totalPages;
	}
	
	public boolean isFirst() {
		return currentPage == 1;
	}
	
	public boolean isLast() {
		return currentPage == totalPages;
	}

	public void setRecords(Collection<T> records) {
		this.records = records;
	}

	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
		calculateTotalPages();
	}

	
}
